package com.nodusk.controller;

import com.nodusk.model.Response;
import com.nodusk.model.TableClass;
import com.nodusk.service.GenerateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;


@RestController
public class GenerateCodeController {
    @Autowired
    private GenerateCodeService generateCodeService;

    @PostMapping("/generateCode")
    public Response generateCode(@RequestBody List<TableClass> tableClassList) {
        try {
            String path = new File(ResourceUtils.getURL("classpath:").getPath()).getAbsolutePath().replace("\\target\\classes", "");
            return generateCodeService.generateCode(tableClassList, path);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return Response.error("代码生成失败");

    }
}
